package com.example.controller;


import com.example.dto.StuDto;
import com.example.pojo.Category;
import com.example.pojo.Dormitory;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.CategoryService;
import com.example.service.DormitoryService;
import com.example.service.UserService;
import com.example.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DormitoryService dormitoryService;

    @PostMapping("/login")
    public Result loginStu(@RequestBody StuDto stuDto){
        Integer id = stuDto.getId();
        String name = stuDto.getName();
        User emp = userService.findUser(name);
        if(emp==null){
            return Result.success("没有这个学生");
        }else{
            if(emp.getId()==id){
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", emp.getId());
                claims.put("name", emp.getName());
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                return Result.error("密码错误");
            }
        }
    }

    @GetMapping("/findCategory")
    public Result listStu(){
        List<Category> list = categoryService.findAll();
        for (Category category : list) {
            System.out.println(category);
        }
        return Result.success(list);
    }

    @GetMapping("/findDormitory")
    public Result listStuDormitory(){
        List<Dormitory> list = dormitoryService.findAll();
        return Result.success(list);
    }

}
